const api = require('../../utils/api.js');
const app = getApp();

let p = 1;

Page({
  data: {
    id: null,
    idx: 1,
    level: 0,
    comment_list: [],
    comment_count: {
      score_all: 0,
      score_6: 0,
      score_3: 0,
      score_2: 0,
      score_1: 0,
    },
    attachment_url: app.siteInfo.attachment_url,
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-19.png',
      text: '暂无评价',
    },
    commentlist: [],
    cPage: 1,
    cMore: true,
    mode: 'normal',
  },
  onLoad(op) {
    let options = op || {};
    let that = this;
    that.setData(
      {
        opts: options,
        id: options.id,
        entry: options.entry,
      },
      () => {
        that.getComments(that.data.id, 0, 1);
      }
    );
  },

  switchComment(e) {
    let { idx, level } = e.currentTarget.dataset;
    p = 1;
    this.setData({
      comment_list: [],
      idx,
      level,
    });
    this.getComments(this.data.id, level, p);
  },

  getComments(id, score, page) {
    let _this = this;
    let entry = _this.data.entry;
    let url = '';
    let data = {};
    let commentListKey = '';
    switch (entry) {
      case 'pt':
        url = api.group.comment;
        data = {
          gid: id,
          score,
          page,
        };
        commentListKey = 'comment';
        break;
      case 'ms':
        url = api.miaosha.comment_list;
        data = {
          goods_id: id,
          score,
          page,
        };
        commentListKey = 'list';
        break;
      default:
        url = api.default.comment_list;
        data = {
          goods_id: id,
          score,
          page,
        };
        commentListKey = 'list';
        break;
    }
    app.ws_micro_shoprequest({
      url,
      method: 'GET',
      data,
      success: function(res) {
        wx.hideLoading();
        if (res.code === 0) {
          let { comment_count, good_per, star_sum } = res.data;
          let list = res.data[commentListKey];
          if (list.length > 0) {
            _this.setData({
              comment_count,
              comment_list: _this.data.comment_list.concat(list),
              good_per,
              star_sum,
            });
          } else {
            if (page > 1) {
              // wx.showToast({
              //   title: '没有更多数据了',
              //   duration: 1500,
              //   icon: 'none',
              // });
            }
          }
        }
      },
    });
  },

  onReachBottom() {
    p++;
    this.getComments(this.data.id, this.data.level, p);
  },

  loadCommentList() {
    let that = this;
    let { commentlist, cPage, cMore, mainid } = that.data;
    if (cMore) {
      app.ws_micro_shoprequest({
        url: api.default.getcomment,
        data: {
          id: mainid,
          page: cPage,
          limit: 20,
        },
        success: function(res) {
          if (res.code === 0) {
            let list = res.data.list;
            if (list.length > 0) {
              cPage += 1;
              that.setData({
                commentlist: commentlist.concat(list),
                cPage,
              });
            } else {
              that.setData({
                cMore: false
              });
              // wx.showToast({
              //   title: "没有更多评论了",
              //   icon: "none"
              // });
            }
          } else {
            wx.showToast({
              title: res.msg || '',
              icon: 'none',
            });
          }
        },
      });
    }
  },
});
